<?php

namespace App\Kuafu\CustomPayment\Model;

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Base\Model\Traits\KfSplitRelation;
use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\User\Model\User;
use App\Scope\ShopScope;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasOne;

class CustomPaymentShopMappingsModel extends BaseModel
{
    use KfSoftDeletes, KfSplitRelation, KfUid;

    protected $connection = 'mysql';
    protected $table      = 'custom_payment_shop_mappings';



    public function createdByUidRel(): HasOne
    {
        return $this->hasOne(User::class, 'id', 'createdByUid')->select('id', 'username');
    }

    public function updatedByUidRel(): HasOne
    {
        return $this->hasOne(User::class, 'id', 'updatedByUid')->select(['id', 'username']);
    }

    public function customPaymentRel(): HasOne
    {
        return $this->hasOne(CustomPaymentModel::class, 'id', 'payId');
    }

    public function shopRel(): HasOne
    {
        return $this->hasOne(Shop::class, 'id', 'shopId');
    }

    /**
     * @param $pageSize
     * @param $pageNum
     * @param $name
     * @param $shopId
     * @param string $startAt
     * @param string $endAt
     * @param bool $withShop
     * @param bool $withDeleted
     * @return LengthAwarePaginator
     */
    public static function getLists(
        $pageSize,
        $pageNum,
        $name,
        $shopId,
        string $startAt = '',
        string $endAt = '',
        bool $withShop = false,
        bool $withDeleted = false
    ): LengthAwarePaginator {
        return self::query()
                   ->with('customPaymentRel')
                   ->with('createdByUidRel')
                   ->with('updatedByUidRel')
                   ->when($withDeleted, function ($query) {
                       $query->withoutGlobalScope('isDel');
                   })
                   ->when(strlen($startAt), function ($query) use ($startAt) {
                       $query->where('createdAt', '>', strtotime($startAt));
                   })
                   ->when(strlen($endAt), function ($query) use ($endAt) {
                       $query->where('createdAt', '<', strtotime($endAt));
                   })
                   ->when($withShop, function ($query) {
                       $query->with('shopRel', function ($query) {
                           $query->select(['id', 'name']);
                       });
                   })
                   ->when(ShopScope::getShopIdsArr(), function ($query) use ($shopId) {
                       $query->whereIn('shopId', ShopScope::getShopIdsArr());
                   })
                   ->when($name, function ($query) use ($name) {
                       $query->whereHas('customPaymentRel', function ($query) use ($name) {
                           $query->where('name', 'like', "%{$name}%");
                       });
                   })
                   ->orderBy('id', 'desc')
                   ->paginate($pageSize, ['*'], 'page', $pageNum);
    }

}
